import request from '../utils/request'

export const topicApi = {
  /**
   * 获取指定圈子
   *
   * @param topicId 需要获取的圈子 ID，多个使用 , 符号进行分割。
   */
  getTopics(topicId: number | string): Promise<DomoTopic[]> {
    const id = String(topicId)
    return request.get('/feed/topics', { params: { id } })
  },

  /** 获取用户圈子 */
  getUserTopics(params: GetTopicParam): Promise<DomoTopic[]> {
    return request.get('/feed/topics', { params })
  },
  /** 获取我关注的圈子 */
  getFollowedTopics(params: QueryParamsWithAfter = {}): Promise<DomoTopic[]> {
    return request.get('/user/followed-topics', { params })
  },

  /** 获取圈子详情 */
  getTopicDetail(topicId: number): Promise<DomoTopic> {
    // const params = { id: topicId }
    return request.get(`/feed/topics/${topicId}`)
  },

  /** 获取圈子的动态 */
  getTopicFeeds(topicId: number, params: GetTopicFeedsParams = {}): Promise<DomoFeedWithIndex[]> {
    return request.get(`/feed/topics/${topicId}/feeds`, { params })
  },

  /** 获取圈子打赏榜 */
  getTopicRankUsers(topicId: number, params: QueryParamsWithOffset = {}): Promise<DomoTopicRank[]> {
    return request.get(`/feed/topics/${topicId}/rewards-rank`, { params })
  },

  /** 关注圈子 */
  followTopic(topicId: number): Promise<void> {
    return request.put(`/user/feed-topics/${topicId}`)
  },

  /** 取消关注圈子 */
  unfollowTopic(topicId: number): Promise<void> {
    return request.delete(`/user/feed-topics/${topicId}`)
  },

  /** 创建圈子 */
  createTopic(payload: CreateTopicPayload): Promise<{ id: number; need_review: boolean }> {
    return request.post('/feed/topics', payload)
  },
  /** 更新圈子 */
  updataTopic(
    id: number,
    payload: CreateTopicPayload,
  ): Promise<{ id: number; need_review: boolean }> {
    return request.patch(`/feed/topics/${id}`, payload)
  },
  /** 打赏圈子 */
  rewardTopic(
    topicId: number,
    payload: RewardTopicPayload,
  ): Promise<{
    /** 剩余积分 */
    currency_sum: string
  }> {
    return request.post(`/feed/topics/${topicId}/rewards`, payload)
  },

  /** 获取圈子打赏日志 */
  getTopicRewardLogs(
    topicId: number,
    params = {} as { limit: number; id: number },
  ): Promise<DomoTopicRewardLog[]> {
    return request.get(`/feed/topics/${topicId}/rewards-logs`, { params })
  },
  /** 获取圈子配置 */
  getTopicConfig: (): Promise<TopicConfig> => request.get('/topic-config'),

  /** 获取圈子排行榜 */
  getTopicRanks(params?: GetTopicRanksParams): Promise<GetTopicRanksReturn> {
    return request.get('/topics-rank', { params })
  },
  /** 获取圈子成员信息 */
  getTopicMember(params: any) {
    return request.get(`/feed/topics/${params.id}/followers`, { params })
  },
  /** 获取圈子分类 */
  getTopicClassify() {
    return request.get(`/feed/topics/categories`)
  },
  /** 获取圈子列表 */
  getTopicsList(params: GetTopicParam): Promise<DomoTopic[]> {
    return request.get('/feed/topics', { params })
  },
  /** 获取热门圈子下的推荐列表 */
  getHotTopicsRecommend(params: GetTopicParam): Promise<DomoTopic[]> {
    return request.get('/feed/topic/recommend', { params })
  },
  /** 获取热门圈子 */
  getHotTopics(params: GetTopicParam): Promise<DomoTopic[]> {
    return request.get('/feed/topic/hot', { params })
  },
  /** 需要我审核的申请记录 */
  needAudited(id: number, params: GetNeedAuditedParam) {
    return request.get(`/user/feed-topics/${id}/apply-logs`, { params })
  },
  /** 同意申请加圈 */
  agreeJoin(topicId: number, logId: number) {
    return request.patch(`/user/feed-topics/${topicId}/logs/${logId}`)
  },
  /** 拒绝申请加圈 */
  refuseJoin(topicId: number, logId: number) {
    return request.delete(`/user/feed-topics/${topicId}/logs/${logId}`)
  },
  /** 当前用户是否可以在圈子发言 */
  canPublishInTopic(topicId: number) {
    return request.get(`/user/feed-topics/${topicId}/can-pub`)
  },
  /** 删除圈子 */
  deleteTopic(topicId: number) {
    return request.delete(`/feed/topics/${topicId}`)
  },
  /** 退出圈子 */
  singOutTopic(topicId: number) {
    return request.delete(`/user/feed-topics/${topicId}`)
  },
  /** 检测用户是否可以发布圈子 */
  canCreatTopic() {
    return request.get('/feed/topics/check-can-create')
  },
  /** 设置用户发言权限 */
  setPublish: (id: number, payload: any) =>
    request.patch(`/user/feed-topics/${id}/can-pub`, payload),
}
